package com.insurance.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.insurance.bean.Question;
import com.insurance.service.QuestionService;


@Transactional
public class QuestionServiceBean implements QuestionService {
	@Resource private SessionFactory sessionFactory;

	public void save(Question question){
		sessionFactory.getCurrentSession().persist(question);
	}
	
	public void update(Question question){
		sessionFactory.getCurrentSession().merge(question);
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Question getQuestion(Integer questionid){
		return (Question)sessionFactory.getCurrentSession().get(Question.class, questionid);
	}

	public void delete(Integer questionid){
		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().load(Question.class, questionid));
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Question> getQuestions(){		
		return sessionFactory.getCurrentSession().createQuery("from Question").list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Question> getQuestionsByUser(String userName){		
		return sessionFactory.getCurrentSession().createQuery("from Question where userName="+userName).list();
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Question> getQuestionsTop10(){		
		return sessionFactory.getCurrentSession().createQuery("from Question order by id desc").
		setFirstResult(0).setMaxResults(10).list();
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Question> getQuestionsTop5(){		
		return sessionFactory.getCurrentSession().createQuery("from Question order by id desc").
		setFirstResult(0).setMaxResults(5).list();
	}
}
